package com.hx.photo.config;

import com.lyncode.jtwig.mvc.JtwigViewResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * Created by hx-pc on 16-5-17.
 */
@Configuration
public class MockConfig extends WebMvcConfigurationSupport {

    private static final Logger LOGGER = LoggerFactory.getLogger(MockConfig.class);

    @Value ("${spring.twig.cache:true}")
    private Boolean twigCache;

    @Bean
    public ViewResolver viewResolver() {
        JtwigViewResolver viewResolver = new JtwigViewResolver();
        viewResolver.setPrefix("classpath:html/");
        viewResolver.setSuffix(".html");
        viewResolver.setEncoding("UTF8");
        viewResolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
        if (twigCache == Boolean.FALSE) {
            viewResolver.setCached(false);
        }
        viewResolver.configuration().render();
        return viewResolver;
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/html/**").addResourceLocations("classpath:/html/");
    }
}
